package com.kaiyidiannao.main;


import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentActivity;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.*;
import android.view.inputmethod.InputMethodManager;
import android.widget.*;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.kaiyidiannao.activity.aboutUs.AboutUsActivity;
import com.kaiyidiannao.activity.daydaycoupon.DdcShoppingCarActivity;
import com.kaiyidiannao.activity.login_or_registered.LoginOrRegistered;
import com.kaiyidiannao.activity.login_or_registered.UserLogin;
import com.kaiyidiannao.activity.menu_right.JoinIn;
import com.kaiyidiannao.activity.myFavorites.MyFavoritesActivity;
import com.kaiyidiannao.activity.myFocusOn.MyFocusOnActivity;
import com.kaiyidiannao.activity.myJoin.MyJoinActivity;
import com.kaiyidiannao.activity.softVersion.Config;
import com.kaiyidiannao.activity.softVersion.NetWorkTool;
import com.kaiyidiannao.activity.softVersion.SoftVersionActivity;
import com.kaiyidiannao.activity.two_wei_ma.TwoWeiMa;
import com.kaiyidiannao.activity.user_detail.MyBidding;
import com.kaiyidiannao.activity.user_detail.MyPublish;
import com.kaiyidiannao.activity.user_detail.OrderCheck;
import com.kaiyidiannao.activity.user_detail.UserDetail;
import com.kaiyidiannao.common.util.AppUtils;
import com.kaiyidiannao.data.DataCenter;
import com.kaiyidiannao.https.HttpUtils;
import com.kaiyidiannao.model.bean.UserBean;
import com.kaiyidiannao.view.HomeFragment;
import com.taplinker.core.util.LogUtil;
import com.umeng.analytics.MobclickAgent;
import com.umeng.fb.FeedbackAgent;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;


public class MainActivity extends FragmentActivity implements Observer {
    private TelephonyManager tm;
    public SlidingMenu mSlidingMenu;
    private LinearLayout leftMenu, rightMunu;
    private long interval = 30000;
    private int screenWidth;
    private FeedbackAgent agent;
    private List<UserBean> userBeans = DataCenter.userLogin;
    SharedPreferences sharedPreferences;
    private int newVerCode = 0;
    private String newVerName = "";
    private String TAG = "MainActivity";
    public ProgressDialog pBar;
    private Handler handler = new Handler();
    ImageView shouYeImage;
    ImageView user_detail_image;
    ImageView orderCheck_image;
    ImageView myPublish_image;
    ImageView myBidding_image;
    ImageView myshoppingcar_image;
    ImageView myFavorites_image;
    ImageView myFocusOn_image;
    ImageView myJoin_image;

    TextView shouYeText;
    TextView user_detail_text;
    TextView orderCheck_text;
    TextView myPublish_text;
    TextView myBidding_text;
    TextView myShoppingcar_text;
    TextView myFavorites_text;
    TextView myFocusOn_text;
    TextView myJoin_text;
    RelativeLayout my_focusOn;

    RelativeLayout to_main_home;//返回主界面
    RelativeLayout user_detail;  //用户详情
    RelativeLayout orderCheck;
    RelativeLayout myPublish;
    RelativeLayout myBidding;
    RelativeLayout myShoppingCar;  //购物车
    RelativeLayout myFavorites;
    RelativeLayout myJoin;
    private static boolean clickFirst = false;
    private static final int time = 2000;
    private static Handler myHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            clickFirst = false;
        }
    };


    @Override
    public void onCreate(Bundle savedInstanceState) {
//		ShareSDK.initSDK(this);
        super.onCreate(savedInstanceState);
        tm = (TelephonyManager) this.getSystemService(TELEPHONY_SERVICE);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        getSupportFragmentManager().beginTransaction().replace(R.id.main_rl, new HomeFragment()).commit();
/*
    tip:  此处逻辑有问题，如果没联网，程序会挂,要修改
 */
        init();
        if (AppUtils.getNetState(this) == AppUtils.NETWORK_OK) {

        } else {
            AppUtils.setNetworkMethod(this);
        }
//        Date date = new Date();
//        Log.d("date",date.getYear()+1900+"-=-=-=-=-");
//
//        getServerVer();
//        if (getServerVerCode()) {
//            int vercode = Config.getVerCode(this);
//            if (newVerCode > vercode) {
//                doNewVersionUpdate();
//            } else {
//                notNewVersionShow();
//            }
//        }

        MobclickAgent.setSessionContinueMillis(interval);
        // 添加观察者
        DataCenter.getInstance().addObserver(this);

//        sharedPreferences = getSharedPreferences("user_detail", Context.MODE_PRIVATE);
//        Log.d("username", sharedPreferences.getString("username", ""));
//        String username = sharedPreferences.getString("username", "");
//        String userid = sharedPreferences.getString("userid", "");
//        String truename = sharedPreferences.getString("truename", "");
//        String qq = sharedPreferences.getString("qq", "");
//        String address = sharedPreferences.getString("address", "");
//        String mobile = sharedPreferences.getString("mobile", "");
//        String password = sharedPreferences.getString("password", "");
//
//        if (userid.equals("")) {
//
//        } else {
//            DataCenter.userBean.setQq(qq);
//            Log.d("bbbbbbbbbbbbbb", DataCenter.userBean.getQq() + "-------------");
//            DataCenter.userBean.setUsername(username);
//            DataCenter.userBean.setTruename(truename);
//            DataCenter.userBean.setUserid(userid);
//            DataCenter.userBean.setAddress(address);
//            DataCenter.userBean.setMobile(mobile);
//            DataCenter.userBean.setPassword(password);
//            DataCenter.userLogin.clear();
//            DataCenter.userBeans.clear();
//            DataCenter.userLogin.add(DataCenter.userBean);
//            DataCenter.userBeans.add(DataCenter.userBean);
//        }
//        Intent intent = getIntent();
//        String flag = intent.getStringExtra("flag");
//        String forget = intent.getStringExtra("forget");
//        if (flag != null) {
//            return;
//        } else {
//            if(forget != null){
//                if(forget.equals("forget")){
//                    return;
//                }
//            }
//            new GetLogin(mobile, password, "1").execute();
//        }
    }


    private void init() {
        createSlidingmenu();
        leftMenuData();
        rightMenuData();
    }

    @Override
    protected void onResume() {
        super.onResume();
        checkColor();
        MobclickAgent.onPageStart("SplashScreen");
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("SplashScreen");
        MobclickAgent.onPause(this);
    }

//    private boolean getServerVer () {
//        try {
//            String verjson = NetWorkTool.getContent(Config.UPDATE_SERVER
//                    + Config.UPDATE_VERJSON);
//            JSONArray array = new JSONArray(verjson);
//            if (array.length() > 0) {
//                JSONObject obj = array.getJSONObject(0);
//                try {
//                    newVerCode = Integer.parseInt(obj.getString("verCode"));
//                    newVerName = obj.getString("verName");
//                } catch (Exception e) {
//                    newVerCode = -1;
//                    newVerName = "";
//                    return false;
//                }
//            }
//        } catch (Exception e) {
//            Log.e(TAG, e.getMessage()+"");
//            return false;
//        }
//        return true;
//    }

    /**
     * 侧滑属性设置
     */
    public void createSlidingmenu() {
        WindowManager manage = getWindowManager();
        Display display = manage.getDefaultDisplay();
        screenWidth = (int) (display.getWidth() * 0.6);
        Log.v("hzw++", screenWidth + "");
        mSlidingMenu = new SlidingMenu(this);
        mSlidingMenu.setMode(SlidingMenu.LEFT_RIGHT);// 设置在屏幕上从左往右划出slidingmenu
//		mSlidingMenu.setShadowDrawable(R.drawable.shadow);// 设置边界的阴影
//		mSlidingMenu.setShadowWidthRes(R.dimen.shadow_width);// 设置阴影宽度
        //		mSlidingMenu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
        mSlidingMenu.setBehindWidth(screenWidth);
        // 设置手势模式
        mSlidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);// 设置从屏幕上滑动范围
        // 设置淡入淡出的比例
        mSlidingMenu.setFadeDegree(0.35f);
        mSlidingMenu.setMenu(R.layout.left_menu);// 设置左边slidingmenu的布局
        mSlidingMenu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);// 将slidingmenu附加到activity上
        // 设置滑动时菜单的是否淡入淡出
        mSlidingMenu.setFadeEnabled(true);
        // 设置右边的布局
        mSlidingMenu.setSecondaryMenu(R.layout.right_menu);
        // 右边添加阴影
//		mSlidingMenu.setSecondaryShadowDrawable(R.drawable.shadowright);
    }

    /**
     * 左侧menu
     */
    public void checkColor() {
        shouYeImage = (ImageView) findViewById(R.id.shouYeImage);
        user_detail_image = (ImageView) findViewById(R.id.user_detail_image);
        orderCheck_image = (ImageView) findViewById(R.id.orderCheck_image);
        myPublish_image = (ImageView) findViewById(R.id.myPublish_image);
        myBidding_image = (ImageView) findViewById(R.id.myBidding_image);
        myshoppingcar_image = (ImageView) findViewById(R.id.myshoppingcar_image);
        myFavorites_image = (ImageView) findViewById(R.id.myFavorites_image);
        myFocusOn_image = (ImageView) findViewById(R.id.myFocusOn_image);
        myJoin_image = (ImageView) findViewById(R.id.myJoin_image);
        shouYeText = (TextView) findViewById(R.id.shouYeText);
        user_detail_text = (TextView) findViewById(R.id.user_detail_text);
        orderCheck_text = (TextView) findViewById(R.id.orderCheck_text);
        myPublish_text = (TextView) findViewById(R.id.myPublish_text);
        myBidding_text = (TextView) findViewById(R.id.myBidding_text);
        myFavorites_text = (TextView) findViewById(R.id.myFavorites_text);
        myFocusOn_text = (TextView) findViewById(R.id.myFocusOn_text);
        myJoin_text = (TextView) findViewById(R.id.myJoin_text);
        my_focusOn = (RelativeLayout) findViewById(R.id.myFocusOn);
        to_main_home = (RelativeLayout) findViewById(R.id.to_main_home);
        user_detail = (RelativeLayout) findViewById(R.id.user_detail);
        orderCheck = (RelativeLayout) findViewById(R.id.orderCheck);
        myPublish = (RelativeLayout) findViewById(R.id.myPublish);
        myBidding = (RelativeLayout) findViewById(R.id.myBidding);
        myShoppingCar = (RelativeLayout) findViewById(R.id.myshoppingcar);
        myShoppingcar_text = (TextView) findViewById(R.id.myShoppingcar_text);
        myFavorites = (RelativeLayout) findViewById(R.id.myFavorites);
        myJoin = (RelativeLayout) findViewById(R.id.myJoin);

        if (DataCenter.backgroundTip == 0) {
            to_main_home.setBackgroundResource(R.drawable.left_menu_selected_bg);
            shouYeText.setTextColor(Color.WHITE);
            shouYeImage.setImageResource(R.drawable.left_menu_home);
        } else if (DataCenter.backgroundTip == 1) {
            user_detail.setBackgroundResource(R.drawable.left_menu_selected_bg);
            user_detail_text.setTextColor(Color.WHITE);
            user_detail_image.setImageResource(R.drawable.left_menu_user);
        } else if (DataCenter.backgroundTip == 2) {
            orderCheck.setBackgroundResource(R.drawable.left_menu_selected_bg);
            orderCheck_text.setTextColor(Color.WHITE);
            orderCheck_image.setImageResource(R.drawable.lef_menu_clipboard);
        } else if (DataCenter.backgroundTip == 3) {
            myPublish.setBackgroundResource(R.drawable.left_menu_selected_bg);
            myPublish_text.setTextColor(Color.WHITE);
            myPublish_image.setImageResource(R.drawable.left_menu_filter);
        } else if (DataCenter.backgroundTip == 4) {
            myBidding.setBackgroundResource(R.drawable.left_menu_selected_bg);
            myBidding_text.setTextColor(Color.WHITE);
            myBidding_image.setImageResource(R.drawable.left_menu_bill);
        } else if (DataCenter.backgroundTip == 5) {
            myShoppingCar.setBackgroundResource(R.drawable.left_menu_selected_bg);
            myShoppingcar_text.setTextColor(Color.WHITE);
            myshoppingcar_image.setImageResource(R.drawable.left_menu_car);
        } else if (DataCenter.backgroundTip == 6) {
            myFavorites.setBackgroundResource(R.drawable.left_menu_selected_bg);
            myFavorites_text.setTextColor(Color.WHITE);
            myFavorites_image.setImageResource(R.drawable.left_menu_favourite);
        } else if (DataCenter.backgroundTip == 7) {
            myJoin.setBackgroundResource(R.drawable.left_menu_selected_bg);
            myJoin_text.setTextColor(Color.WHITE);
            myJoin_image.setImageResource(R.drawable.myjoin_bai);
        } else if (DataCenter.backgroundTip == 8) {
            my_focusOn.setBackgroundResource(R.drawable.left_menu_selected_bg);
            myFocusOn_text.setTextColor(Color.WHITE);
            myFocusOn_image.setImageResource(R.drawable.left_menu_eye);
        }

    }


    public void leftMenuData() {
        leftMenu = (LinearLayout) mSlidingMenu.getMenu();
        TextView button = (TextView) findViewById(R.id.left_menu_quitButton); //注销
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, LoginOrRegistered.class);
                startActivity(intent);
            }
        });

        checkColor();
        to_main_home.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mSlidingMenu.toggle();//切换左边
                shouYeImage.setImageResource(R.drawable.left_menu_home);
                user_detail_image.setImageResource(R.drawable.left_menu_user_nomal);
                orderCheck_image.setImageResource(R.drawable.lef_menu_clipboard_nomal);
                myPublish_image.setImageResource(R.drawable.left_menu_filter_nomal);
                myBidding_image.setImageResource(R.drawable.left_menu_bill_nomal);
                myshoppingcar_image.setImageResource(R.drawable.left_menu_car_nomal);
                myFavorites_image.setImageResource(R.drawable.left_menu_favourite_nomal);
                myFocusOn_image.setImageResource(R.drawable.left_menu_eye_nomal);
                myJoin_image.setImageResource(R.drawable.myjoin_hei);

                to_main_home.setBackgroundResource(R.drawable.left_menu_selected_bg);
                user_detail.setBackgroundResource(R.color.font_white);
                orderCheck.setBackgroundResource(R.color.font_white);
                myPublish.setBackgroundResource(R.color.font_white);
                myBidding.setBackgroundResource(R.color.font_white);
                myShoppingCar.setBackgroundResource(R.color.font_white);
                myFavorites.setBackgroundResource(R.color.font_white);
                myJoin.setBackgroundResource(R.color.font_white);

                my_focusOn.setBackgroundResource(R.color.font_white);
                myFocusOn_text.setTextColor(Color.rgb(143, 143, 143));

                shouYeText.setTextColor(Color.WHITE);
                user_detail_text.setTextColor(Color.rgb(143, 143, 143));
                orderCheck_text.setTextColor(Color.rgb(143, 143, 143));
                myPublish_text.setTextColor(Color.rgb(143, 143, 143));
                myBidding_text.setTextColor(Color.rgb(143, 143, 143));
                myShoppingcar_text.setTextColor(Color.rgb(143, 143, 143));
                myFavorites_text.setTextColor(Color.rgb(143, 143, 143));
                myJoin_text.setTextColor(Color.rgb(143, 143, 143));
                DataCenter.backgroundTip = 0;
            }
        });

        user_detail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (userBeans.size() == 0) {
                    Intent intent = new Intent(MainActivity.this, LoginOrRegistered.class);
                    startActivity(intent);
                    Toast.makeText(MainActivity.this, "请先登录", Toast.LENGTH_SHORT).show();
                } else {
                    Intent intent = new Intent(MainActivity.this, UserDetail.class);
                    startActivity(intent);
                }

                shouYeImage.setImageResource(R.drawable.left_menu_home_nomal);
                user_detail_image.setImageResource(R.drawable.left_menu_user);
                orderCheck_image.setImageResource(R.drawable.lef_menu_clipboard_nomal);
                myPublish_image.setImageResource(R.drawable.left_menu_filter_nomal);
                myBidding_image.setImageResource(R.drawable.left_menu_bill_nomal);
                myshoppingcar_image.setImageResource(R.drawable.left_menu_car_nomal);
                myFavorites_image.setImageResource(R.drawable.left_menu_favourite_nomal);
                myFocusOn_image.setImageResource(R.drawable.left_menu_eye_nomal);
                myJoin_image.setImageResource(R.drawable.myjoin_hei);


                to_main_home.setBackgroundResource(R.color.font_white);
                user_detail.setBackgroundResource(R.drawable.left_menu_selected_bg);
                orderCheck.setBackgroundResource(R.color.font_white);
                myPublish.setBackgroundResource(R.color.font_white);
                myBidding.setBackgroundResource(R.color.font_white);
                myShoppingCar.setBackgroundResource(R.color.font_white);
                myFavorites.setBackgroundResource(R.color.font_white);
                myJoin.setBackgroundResource(R.color.font_white);

                my_focusOn.setBackgroundResource(R.color.font_white);
                myFocusOn_text.setTextColor(Color.rgb(143, 143, 143));

                shouYeText.setTextColor(Color.rgb(143, 143, 143));
                user_detail_text.setTextColor(Color.WHITE);
                orderCheck_text.setTextColor(Color.rgb(143, 143, 143));
                myPublish_text.setTextColor(Color.rgb(143, 143, 143));
                myBidding_text.setTextColor(Color.rgb(143, 143, 143));
                myShoppingcar_text.setTextColor(Color.rgb(143, 143, 143));
                myFavorites_text.setTextColor(Color.rgb(143, 143, 143));
                myJoin_text.setTextColor(Color.rgb(143, 143, 143));

                DataCenter.backgroundTip = 1;
            }
        });

        orderCheck.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (userBeans.size() == 0) {
                    Intent intent = new Intent(MainActivity.this, LoginOrRegistered.class);
                    startActivity(intent);
                    Toast.makeText(MainActivity.this, "请先登录", Toast.LENGTH_SHORT).show();
                } else {
                    Intent intent = new Intent(MainActivity.this, OrderCheck.class);
                    intent.putExtra("orderCheck", "orderCheck");
                    startActivity(intent);
                }

                shouYeImage.setImageResource(R.drawable.left_menu_home_nomal);
                user_detail_image.setImageResource(R.drawable.left_menu_user_nomal);
                orderCheck_image.setImageResource(R.drawable.lef_menu_clipboard);
                myPublish_image.setImageResource(R.drawable.left_menu_filter_nomal);
                myBidding_image.setImageResource(R.drawable.left_menu_bill_nomal);
                myshoppingcar_image.setImageResource(R.drawable.left_menu_car_nomal);
                myFavorites_image.setImageResource(R.drawable.left_menu_favourite_nomal);
                myFocusOn_image.setImageResource(R.drawable.left_menu_eye_nomal);
                myJoin_image.setImageResource(R.drawable.myjoin_hei);


                to_main_home.setBackgroundResource(R.color.font_white);
                user_detail.setBackgroundResource(R.color.font_white);
                orderCheck.setBackgroundResource(R.drawable.left_menu_selected_bg);
                myPublish.setBackgroundResource(R.color.font_white);
                myBidding.setBackgroundResource(R.color.font_white);
                myShoppingCar.setBackgroundResource(R.color.font_white);
                myFavorites.setBackgroundResource(R.color.font_white);
                myJoin.setBackgroundResource(R.color.font_white);

                my_focusOn.setBackgroundResource(R.color.font_white);
                myFocusOn_text.setTextColor(Color.rgb(143, 143, 143));

                shouYeText.setTextColor(Color.rgb(143, 143, 143));
                user_detail_text.setTextColor(Color.rgb(143, 143, 143));
                orderCheck_text.setTextColor(Color.WHITE);
                myPublish_text.setTextColor(Color.rgb(143, 143, 143));
                myBidding_text.setTextColor(Color.rgb(143, 143, 143));
                myShoppingcar_text.setTextColor(Color.rgb(143, 143, 143));
                myFavorites_text.setTextColor(Color.rgb(143, 143, 143));
                myJoin_text.setTextColor(Color.rgb(143, 143, 143));
                DataCenter.backgroundTip = 2;
            }
        });

        myPublish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (userBeans.size() == 0) {
                    Intent intent = new Intent(MainActivity.this, LoginOrRegistered.class);
                    startActivity(intent);
                    Toast.makeText(MainActivity.this, "请先登录", Toast.LENGTH_SHORT).show();
                } else {
                    Intent intent = new Intent(MainActivity.this, MyPublish.class);
                    startActivity(intent);
                }

                shouYeImage.setImageResource(R.drawable.left_menu_home_nomal);
                user_detail_image.setImageResource(R.drawable.left_menu_user_nomal);
                orderCheck_image.setImageResource(R.drawable.lef_menu_clipboard_nomal);
                myPublish_image.setImageResource(R.drawable.left_menu_filter);
                myBidding_image.setImageResource(R.drawable.left_menu_bill_nomal);
                myshoppingcar_image.setImageResource(R.drawable.left_menu_car_nomal);
                myFavorites_image.setImageResource(R.drawable.left_menu_favourite_nomal);
                myFocusOn_image.setImageResource(R.drawable.left_menu_eye_nomal);
                myJoin_image.setImageResource(R.drawable.myjoin_hei);


                to_main_home.setBackgroundResource(R.color.font_white);
                user_detail.setBackgroundResource(R.color.font_white);
                orderCheck.setBackgroundResource(R.color.font_white);
                myPublish.setBackgroundResource(R.drawable.left_menu_selected_bg);
                myBidding.setBackgroundResource(R.color.font_white);
                myShoppingCar.setBackgroundResource(R.color.font_white);
                myFavorites.setBackgroundResource(R.color.font_white);
                myJoin.setBackgroundResource(R.color.font_white);

                my_focusOn.setBackgroundResource(R.color.font_white);
                myFocusOn_text.setTextColor(Color.rgb(143, 143, 143));

                shouYeText.setTextColor(Color.rgb(143, 143, 143));
                user_detail_text.setTextColor(Color.rgb(143, 143, 143));
                orderCheck_text.setTextColor(Color.rgb(143, 143, 143));
                myPublish_text.setTextColor(Color.WHITE);
                myBidding_text.setTextColor(Color.rgb(143, 143, 143));
                myShoppingcar_text.setTextColor(Color.rgb(143, 143, 143));
                myFavorites_text.setTextColor(Color.rgb(143, 143, 143));
                myJoin_text.setTextColor(Color.rgb(143, 143, 143));
                DataCenter.backgroundTip = 3;
            }
        });

        myBidding.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (userBeans.size() == 0) {
                    Intent intent = new Intent(MainActivity.this, LoginOrRegistered.class);
                    startActivity(intent);
                    Toast.makeText(MainActivity.this, "请先登录", Toast.LENGTH_SHORT).show();
                } else {
                    Intent intent = new Intent(MainActivity.this, MyBidding.class);
                    startActivity(intent);
                }

                shouYeImage.setImageResource(R.drawable.left_menu_home_nomal);
                user_detail_image.setImageResource(R.drawable.left_menu_user_nomal);
                orderCheck_image.setImageResource(R.drawable.lef_menu_clipboard_nomal);
                myPublish_image.setImageResource(R.drawable.left_menu_filter_nomal);
                myBidding_image.setImageResource(R.drawable.left_menu_bill);
                myshoppingcar_image.setImageResource(R.drawable.left_menu_car_nomal);
                myFavorites_image.setImageResource(R.drawable.left_menu_favourite_nomal);
                myFocusOn_image.setImageResource(R.drawable.left_menu_eye_nomal);
                myJoin_image.setImageResource(R.drawable.myjoin_hei);


                to_main_home.setBackgroundResource(R.color.font_white);
                user_detail.setBackgroundResource(R.color.font_white);
                orderCheck.setBackgroundResource(R.color.font_white);
                myPublish.setBackgroundResource(R.color.font_white);
                myBidding.setBackgroundResource(R.drawable.left_menu_selected_bg);
                myShoppingCar.setBackgroundResource(R.color.font_white);
                myFavorites.setBackgroundResource(R.color.font_white);
                myJoin.setBackgroundResource(R.color.font_white);

                my_focusOn.setBackgroundResource(R.color.font_white);
                myFocusOn_text.setTextColor(Color.rgb(143, 143, 143));

                shouYeText.setTextColor(Color.rgb(143, 143, 143));
                user_detail_text.setTextColor(Color.rgb(143, 143, 143));
                orderCheck_text.setTextColor(Color.rgb(143, 143, 143));
                myPublish_text.setTextColor(Color.rgb(143, 143, 143));
                myBidding_text.setTextColor(Color.WHITE);
                myShoppingcar_text.setTextColor(Color.rgb(143, 143, 143));
                myFavorites_text.setTextColor(Color.rgb(143, 143, 143));
                myJoin_text.setTextColor(Color.rgb(143, 143, 143));
                DataCenter.backgroundTip = 4;

            }
        });

        myShoppingCar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (userBeans.size() == 0) {
                    Intent intent = new Intent(MainActivity.this, LoginOrRegistered.class);
                    startActivity(intent);
                    Toast.makeText(MainActivity.this, "请先登录", Toast.LENGTH_SHORT).show();
                } else {
                    Intent intent = new Intent(MainActivity.this, DdcShoppingCarActivity.class);
                    startActivity(intent);
                }

                shouYeImage.setImageResource(R.drawable.left_menu_home_nomal);
                user_detail_image.setImageResource(R.drawable.left_menu_user_nomal);
                orderCheck_image.setImageResource(R.drawable.lef_menu_clipboard_nomal);
                myPublish_image.setImageResource(R.drawable.left_menu_filter_nomal);
                myBidding_image.setImageResource(R.drawable.left_menu_bill_nomal);
                myshoppingcar_image.setImageResource(R.drawable.left_menu_car);
                myFavorites_image.setImageResource(R.drawable.left_menu_favourite_nomal);
                myFocusOn_image.setImageResource(R.drawable.left_menu_eye_nomal);
                myJoin_image.setImageResource(R.drawable.myjoin_hei);

                to_main_home.setBackgroundResource(R.color.font_white);
                user_detail.setBackgroundResource(R.color.font_white);
                orderCheck.setBackgroundResource(R.color.font_white);
                myPublish.setBackgroundResource(R.color.font_white);
                myBidding.setBackgroundResource(R.color.font_white);
                myShoppingCar.setBackgroundResource(R.drawable.left_menu_selected_bg);
                myFavorites.setBackgroundResource(R.color.font_white);
                myJoin.setBackgroundResource(R.color.font_white);

                my_focusOn.setBackgroundResource(R.color.font_white);
                myFocusOn_text.setTextColor(Color.rgb(143, 143, 143));

                shouYeText.setTextColor(Color.rgb(143, 143, 143));
                user_detail_text.setTextColor(Color.rgb(143, 143, 143));
                orderCheck_text.setTextColor(Color.rgb(143, 143, 143));
                myPublish_text.setTextColor(Color.rgb(143, 143, 143));
                myBidding_text.setTextColor(Color.rgb(143, 143, 143));
                myShoppingcar_text.setTextColor(Color.WHITE);
                myFavorites_text.setTextColor(Color.rgb(143, 143, 143));
                myJoin_text.setTextColor(Color.rgb(143, 143, 143));
                DataCenter.backgroundTip = 5;

            }
        });

        myFavorites.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (userBeans.size() == 0) {
                    Intent intent = new Intent(MainActivity.this, LoginOrRegistered.class);
                    startActivity(intent);
                    Toast.makeText(MainActivity.this, "请先登录", Toast.LENGTH_SHORT).show();
                } else {
                    Intent intent = new Intent(MainActivity.this, MyFavoritesActivity.class);
                    startActivity(intent);
                }

                shouYeImage.setImageResource(R.drawable.left_menu_home_nomal);
                user_detail_image.setImageResource(R.drawable.left_menu_user_nomal);
                orderCheck_image.setImageResource(R.drawable.lef_menu_clipboard_nomal);
                myPublish_image.setImageResource(R.drawable.left_menu_filter_nomal);
                myBidding_image.setImageResource(R.drawable.left_menu_bill_nomal);
                myshoppingcar_image.setImageResource(R.drawable.left_menu_car_nomal);
                myFavorites_image.setImageResource(R.drawable.left_menu_favourite);
                myFocusOn_image.setImageResource(R.drawable.left_menu_eye_nomal);
                myJoin_image.setImageResource(R.drawable.myjoin_hei);

                to_main_home.setBackgroundResource(R.color.font_white);
                user_detail.setBackgroundResource(R.color.font_white);
                orderCheck.setBackgroundResource(R.color.font_white);
                myPublish.setBackgroundResource(R.color.font_white);
                myBidding.setBackgroundResource(R.color.font_white);
                myShoppingCar.setBackgroundResource(R.color.font_white);
                myFavorites.setBackgroundResource(R.drawable.left_menu_selected_bg);
                myJoin.setBackgroundResource(R.color.font_white);
                my_focusOn.setBackgroundResource(R.color.font_white);


                myFocusOn_text.setTextColor(Color.rgb(143, 143, 143));
                shouYeText.setTextColor(Color.rgb(143, 143, 143));
                user_detail_text.setTextColor(Color.rgb(143, 143, 143));
                orderCheck_text.setTextColor(Color.rgb(143, 143, 143));
                myPublish_text.setTextColor(Color.rgb(143, 143, 143));
                myBidding_text.setTextColor(Color.rgb(143, 143, 143));
                myShoppingcar_text.setTextColor(Color.rgb(143, 143, 143));
                myFavorites_text.setTextColor(Color.WHITE);
                myJoin_text.setTextColor(Color.rgb(143, 143, 143));
                DataCenter.backgroundTip = 6;
            }
        });

        myJoin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (userBeans.size() == 0) {
                    Intent intent = new Intent(MainActivity.this, LoginOrRegistered.class);
                    startActivity(intent);
                    Toast.makeText(MainActivity.this, "请先登录", Toast.LENGTH_SHORT).show();
                } else {
                    Intent intent = new Intent(MainActivity.this, MyJoinActivity.class);
                    startActivity(intent);
                }

                shouYeImage.setImageResource(R.drawable.left_menu_home_nomal);
                user_detail_image.setImageResource(R.drawable.left_menu_user_nomal);
                orderCheck_image.setImageResource(R.drawable.lef_menu_clipboard_nomal);
                myPublish_image.setImageResource(R.drawable.left_menu_filter_nomal);
                myBidding_image.setImageResource(R.drawable.left_menu_bill_nomal);
                myshoppingcar_image.setImageResource(R.drawable.left_menu_car_nomal);
                myFavorites_image.setImageResource(R.drawable.left_menu_favourite_nomal);
                myFocusOn_image.setImageResource(R.drawable.left_menu_eye_nomal);
                myJoin_image.setImageResource(R.drawable.myjoin_bai);


                to_main_home.setBackgroundResource(R.color.font_white);
                user_detail.setBackgroundResource(R.color.font_white);
                orderCheck.setBackgroundResource(R.color.font_white);
                myPublish.setBackgroundResource(R.color.font_white);
                myBidding.setBackgroundResource(R.color.font_white);
                myShoppingCar.setBackgroundResource(R.color.font_white);
                myFavorites.setBackgroundResource(R.color.font_white);
                myJoin.setBackgroundResource(R.drawable.left_menu_selected_bg);
                my_focusOn.setBackgroundResource(R.color.font_white);
                myFocusOn_text.setTextColor(Color.rgb(143, 143, 143));

                shouYeText.setTextColor(Color.rgb(143, 143, 143));
                user_detail_text.setTextColor(Color.rgb(143, 143, 143));
                orderCheck_text.setTextColor(Color.rgb(143, 143, 143));
                myPublish_text.setTextColor(Color.rgb(143, 143, 143));
                myBidding_text.setTextColor(Color.rgb(143, 143, 143));
                myShoppingcar_text.setTextColor(Color.rgb(143, 143, 143));
                myFavorites_text.setTextColor(Color.rgb(143, 143, 143));
                myJoin_text.setTextColor(Color.WHITE);
                DataCenter.backgroundTip = 7;
            }
        });

        my_focusOn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (userBeans.size() == 0) {
                    Intent intent = new Intent(MainActivity.this, LoginOrRegistered.class);
                    startActivity(intent);
                    Toast.makeText(MainActivity.this, "请先登录", Toast.LENGTH_SHORT).show();
                } else {
                    Intent intent = new Intent(MainActivity.this, MyFocusOnActivity.class);
                    startActivity(intent);
                }

                shouYeImage.setImageResource(R.drawable.left_menu_home_nomal);
                user_detail_image.setImageResource(R.drawable.left_menu_user_nomal);
                orderCheck_image.setImageResource(R.drawable.lef_menu_clipboard_nomal);
                myPublish_image.setImageResource(R.drawable.left_menu_filter_nomal);
                myBidding_image.setImageResource(R.drawable.left_menu_bill_nomal);
                myshoppingcar_image.setImageResource(R.drawable.left_menu_car_nomal);
                myFavorites_image.setImageResource(R.drawable.left_menu_favourite_nomal);
                myFocusOn_image.setImageResource(R.drawable.left_menu_eye);
                myJoin_image.setImageResource(R.drawable.myjoin_hei);


                to_main_home.setBackgroundResource(R.color.font_white);
                user_detail.setBackgroundResource(R.color.font_white);
                orderCheck.setBackgroundResource(R.color.font_white);
                myPublish.setBackgroundResource(R.color.font_white);
                myBidding.setBackgroundResource(R.color.font_white);
                myShoppingCar.setBackgroundResource(R.color.font_white);
                myFavorites.setBackgroundResource(R.color.font_white);
                myJoin.setBackgroundResource(R.color.font_white);
                my_focusOn.setBackgroundResource(R.drawable.left_menu_selected_bg);


                shouYeText.setTextColor(Color.rgb(143, 143, 143));
                user_detail_text.setTextColor(Color.rgb(143, 143, 143));
                orderCheck_text.setTextColor(Color.rgb(143, 143, 143));
                myPublish_text.setTextColor(Color.rgb(143, 143, 143));
                myBidding_text.setTextColor(Color.rgb(143, 143, 143));
                myShoppingcar_text.setTextColor(Color.rgb(143, 143, 143));
                myFavorites_text.setTextColor(Color.rgb(143, 143, 143));
                myJoin_text.setTextColor(Color.rgb(143, 143, 143));
                myFocusOn_text.setTextColor(Color.WHITE);
                DataCenter.backgroundTip = 8;
            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.d("userBeans", userBeans.size() + "");
        if (userBeans.size() == 0) {
            LinearLayout logined = (LinearLayout) findViewById(R.id.logined);
            logined.setVisibility(View.GONE);
            LinearLayout login = (LinearLayout) findViewById(R.id.login);
            login.setVisibility(View.VISIBLE);
            TextView loginButton = (TextView) findViewById(R.id.left_login_button);
            loginButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(MainActivity.this, LoginOrRegistered.class);
                    startActivity(intent);
                }
            });
        } else {
            //登录成功后切换登录按钮为注销
            TextView left_menu_quitButton = (TextView) findViewById(R.id.left_menu_quitButton);
            left_menu_quitButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
//                    sharedPreferences = getSharedPreferences("user_detail", Context.MODE_PRIVATE);
//                    SharedPreferences.Editor editor = sharedPreferences.edit();
//                    editor.clear();
//                    editor.commit();
                    DataCenter.userLogin.clear();
                    DataCenter.userBeans.clear();
                    Intent intent = new Intent(MainActivity.this, LoginOrRegistered.class);
                    startActivity(intent);
                }
            });
            LinearLayout logined = (LinearLayout) findViewById(R.id.logined);
            logined.setVisibility(View.VISIBLE);
            TextView username = (TextView) logined.findViewById(R.id.username);
            TextView mobile = (TextView) logined.findViewById(R.id.mobile);
            ArrayList<UserBean> userBeanList = DataCenter.userBeans;
            Log.d("userBeanList", userBeanList.size() + "----------");
            if (userBeanList.size() != 0) {
                Log.d("aaaaaaaaaaaa", userBeanList.get(0).getUsername() + "------------");
                if (userBeanList.get(0).getUsername() == null || userBeanList.get(0).getUsername().equals("")) {
                    username.setText(userBeanList.get(0).getUserid());
                } else {
                    username.setText(userBeanList.get(0).getUsername());
                }
                mobile.setText(userBeanList.get(0).getMobile());
            }
            LinearLayout login = (LinearLayout) findViewById(R.id.login);
            login.setVisibility(View.GONE);

        }

    }

    /**
     * 右侧MENU
     */
    private void rightMenuData() {
        rightMunu = (LinearLayout) mSlidingMenu.getSecondaryMenu();
        LinearLayout two_wei_ma = (LinearLayout) findViewById(R.id.two_wei_ma);
        two_wei_ma.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Intent intent = new Intent(MainActivity.this, CaptureActivity.class);
                Intent intent = new Intent(MainActivity.this, TwoWeiMa.class);
                startActivity(intent);
            }
        });
        agent = new FeedbackAgent(MainActivity.this);
        LinearLayout feedback = (LinearLayout) findViewById(R.id.feedback);

        feedback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                agent.startFeedbackActivity();
            }
        });

        LinearLayout joinIn = (LinearLayout) findViewById(R.id.joinIn);
        joinIn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (userBeans.size() == 0) {
                    Intent intent = new Intent(MainActivity.this, LoginOrRegistered.class);
                    startActivity(intent);
                    Toast.makeText(MainActivity.this, "请先登录", Toast.LENGTH_SHORT).show();
                } else {
                    Intent intent = new Intent(MainActivity.this, JoinIn.class);
                    startActivity(intent);
                }
            }
        });

        LinearLayout aboutUs = (LinearLayout) findViewById(R.id.aboutUs);
        aboutUs.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, AboutUsActivity.class);
                startActivity(intent);
            }
        });

        LinearLayout soft_version = (LinearLayout) findViewById(R.id.softVersion);
        soft_version.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, SoftVersionActivity.class);
                startActivity(intent);
            }
        });

    }

    public void leftClick(View view) {
        mSlidingMenu.showMenu(true);//切换左边
    }

    public void rightClick(View view) {
        mSlidingMenu.showSecondaryMenu(true);//切换右边
    }

//    @Override
//    public void onBackPressed() {
//        //当fragment堆栈为空且menu展开的情况
//        if (getSupportFragmentManager().getBackStackEntryCount() == 0 && !mSlidingMenu.isMenuShowing()) {
//            AppUtils.exit(this);
//        } else {
//            if (mSlidingMenu.isMenuShowing()) {
//                mSlidingMenu.toggle();
//            } else {
//                super.onBackPressed();
//            }
//        }
//    }

    /**
     * 隐藏输入法面板
     */

    public void hideKeyboard(View v) {
        InputMethodManager imm = ((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE));
        if (imm != null && getCurrentFocus() != null) {
            imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
                    InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }

    //当指定了android:configChanges="orientation"后,方向改变时onConfigurationChanged被调用
//	@Override
//	public void onConfigurationChanged(Configuration newConfig) {
//		super.onConfigurationChanged(newConfig);
//		switch (newConfig.orientation) {
//		case Configuration.ORIENTATION_PORTRAIT:
//			break;
//		case Configuration.ORIENTATION_LANDSCAPE:
//			break;
//		}
//	}
    @Override
    public void update(Observable observable, Object data) {
        DataCenter.flag flag = (DataCenter.flag) data;
        String code = DataCenter.code;
        if (flag == DataCenter.flag.GET_LOGIN_SUCCESS
                || flag == DataCenter.flag.GET_LOGIN_FAILED) { //类表数据


            if (flag == DataCenter.flag.GET_LOGIN_SUCCESS) {
                if (code.equals(DataCenter.successFlag)) {
                    if (HttpUtils.isConnected()) {
                        Toast.makeText(MainActivity.this, DataCenter.msg, Toast.LENGTH_SHORT).show();
                    }
                }
            }

            if (flag == DataCenter.flag.GET_LOGIN_FAILED) {
                if (code.equals(DataCenter.failedFlag)) {
                    Toast.makeText(MainActivity.this, DataCenter.msg, Toast.LENGTH_SHORT).show();
                }
            }
        }

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        LogUtil.d(DataCenter.LOG_INFO, UserLogin.class.getSimpleName() + ".onDestroy");
        //删除观察者
        DataCenter.getInstance().deleteObserver(this);
    }

    private boolean getServerVerCode() {
        try {
            String verjson = NetWorkTool.getContent(Config.UPDATE_SERVER
                    + Config.UPDATE_VERJSON);
            JSONArray array = new JSONArray(verjson);
            if (array.length() > 0) {
                JSONObject obj = array.getJSONObject(0);
                try {
                    newVerCode = Integer.parseInt(obj.getString("verCode"));
                    newVerName = obj.getString("verName");
                } catch (Exception e) {
                    newVerCode = -1;
                    newVerName = "";
                    return false;
                }
            }
        } catch (Exception e) {
            Log.e(TAG, e.getMessage() + "");
            return false;
        }
        return true;
    }

    private void showTips() {
        if (!clickFirst) {
            Toast.makeText(MainActivity.this, "再按一次确认退出", Toast.LENGTH_SHORT).show();
            startProcessTiming();
        } else {
            Intent intent = new Intent(Intent.ACTION_MAIN);
            intent.addCategory(Intent.CATEGORY_HOME);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
            android.os.Process.killProcess(android.os.Process.myPid());
        }
    }

    private static void startProcessTiming() {
        new Thread(new Runnable() {

            @Override
            public void run() {
                clickFirst = true;
                try {
                    Thread.sleep(time);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                myHandler.sendEmptyMessage(0);
            }
        }).start();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            showTips();
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }
}


